package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.common.BaseContext;
import com.project.dtoplus.PersonalInformation;
import com.project.service.LoginService;
import com.project.entity.Login;
import com.project.entity.UserMember;
import com.project.mapper.UserMemberMapper;
import com.project.service.UserMemberService;
import com.project.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Service
@Slf4j
public class UserMemberServiceImpl extends ServiceImpl<UserMemberMapper, UserMember> implements UserMemberService {
    @Autowired
    private LoginService loginService;
    @Autowired
    private UserMemberService userMemberService;


    //添加登录用户
    @Override
    public void add(HttpServletRequest request, UserMember user) {
        SecurityUtil.XcUser userOne = SecurityUtil.getUser();
        //根据session中的userid获取Login对象
//        Long loginId =BaseContext.getCurrentId();
        Long loginId = userOne.getUserId();
        LambdaQueryWrapper<Login> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Login::getUserId, loginId);
        Login byId = loginService.getOne(queryWrapper);

        user.setCreateUserId(byId.getUserId());
        user.setModifiedUserId(byId.getUserId());
        user.setCreateTime(LocalDateTime.now());
        user.setModifiedTime(LocalDateTime.now());

        userMemberService.save(user);
    }

    //获取用户信息
    public PersonalInformation obtain() {
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        System.out.println(user);
        //根据session中的userid获取Login对象
        Long id = user.getUserId();
        System.out.println(id);
        LambdaQueryWrapper<Login> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Login::getUserId, id);
        Login login = loginService.getOne(queryWrapper);
        PersonalInformation personal = this.getPersonal(login);

        return personal;
    }

    //修改密码
    public String changeInfo( Login login) {
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        Long id = user.getUserId();
        LambdaQueryWrapper<Login> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Login::getUserId, id);
        Login one = loginService.getOne(queryWrapper);
        log.info("原:" + one.getPassword());
        log.info(login.getPassword());
        log.info("现:" + login.getPassword());
        if (!one.getPassword().equals(login.getPassword())) {
            return "原密码输入错误";
        }

        if(!login.getModifiedPassword().equals(login.getAgainModifiedPassword())){
            return "密码输入不一致";
        }

            one.setModifiedPassword(login.getModifiedPassword());
            one.setAgainModifiedPassword(login.getAgainModifiedPassword());
            one.setPassword(login.getModifiedPassword());
            one.setModifiedTime(LocalDateTime.now());

            loginService.updateById(one);

            //改user_member表中的密码
            LambdaQueryWrapper<UserMember> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(UserMember::getUserId, one.getUserId());
            UserMember member = userMemberService.getOne(lambdaQueryWrapper);
            member.setPassword(one.getPassword());
            member.setModifiedUserId(one.getUserId());
            userMemberService.updateById(member);

            return "修改密码成功";



    }
    //返回用户首次登录信息的筛选
    public PersonalInformation getPersonal(Login login){
        PersonalInformation personal = new PersonalInformation();
        BeanUtils.copyProperties(login,personal);
        return personal;

    }


}
